package com.yb.web.global;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.announcement.AnnouncementService;
import com.yb.bussiness.announcement.AnnouncementServiceImpl;
import com.yb.bussiness.goods.CategoryService;
import com.yb.bussiness.goods.CategoryServiceImpl;
import com.yb.bussiness.goods.GoodsService;
import com.yb.bussiness.goods.GoodsServiceImpl;
import com.yb.bussiness.goods.MarketService;
import com.yb.bussiness.goods.MarketServiceImpl;
import com.yb.bussiness.goods.TgPartService;
import com.yb.bussiness.goods.TgPartServiceImpl;
import com.yb.bussiness.information.InformationService;
import com.yb.bussiness.information.InformationServiceImpl;
import com.yb.bussiness.shoper.BrandService;
import com.yb.bussiness.shoper.BrandServiceImpl;
import com.yb.bussiness.type.TypeService;
import com.yb.bussiness.type.TypeServiceImpl;
import com.yb.domain.goods.TgInformation;
import com.yb.domain.goods.TgInformationFormBean;
import com.yb.domain.goods.TgMarket;
import com.yb.domain.goods.TgType;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.MemoryData;
import com.yb.util.framework.OnLineCustomers;
import com.yb.util.framework.PageUtil;
import com.yb.util.framework.UserList;

public class StageAction extends BaseAction {

	private static final long serialVersionUID = -969994003674042177L;

	private CategoryService categoryService;
	private BrandService brandService;
	private GoodsService goodService;
	private AnnouncementService annoService;
	private InformationService infoService;
	private MarketService marketService;
	private TypeService typeService;
	private Long categoryId;
	private Long pid;
	private Long typeId;
	private int pageNo;
	private int limit;
	private int pageNo2;
	private int limit2;
    private int total;
    private List rows;
	
	private TgPartService service;

	public StageAction() {
		categoryService = new CategoryServiceImpl();
		brandService = new BrandServiceImpl();
		goodService = new GoodsServiceImpl();
		annoService = new AnnouncementServiceImpl();
		infoService = new InformationServiceImpl();
		typeService = new TypeServiceImpl();
		marketService = new MarketServiceImpl();
		service = new TgPartServiceImpl();
	}

	public String home() {
		try {
			ActionContext context = ActionContext.getContext();
			context.put("categoryList", MemoryData.getCategoryList());
			context.put("brandHotMap", MemoryData.getBrandMap());
			context.put("pingouMap", MemoryData.getPingouMap());
			context.put("informationMap", MemoryData.getInformationMap());
			context.put("hotInfoMap", MemoryData.getHotInforMap());
			context.put("hotGoodMap", MemoryData.getHotGoodMap());
			context.put("totalCount", MemoryData.getTotalCount());
			context.put("hotActivityList", MemoryData.getHotActivityList());
			context.put("imgInfoList", MemoryData.getImgInfoList());
			context.put("hotTrend", MemoryData.getHotTrendList());
			context
					.put("hotTrendCategory", MemoryData
							.getHotTrendCategoryMap());
		} catch (Exception e) {
			log.error("获取首页数据", e);
		}
		return "home";
	}

	public String guide() {
		try {
			TgInformation tgInformationimg = new TgInformation();
			tgInformationimg = infoService.readBycategoryFlag(8);
			ActionContext.getContext()
					.put("tgInformationimg", tgInformationimg);
			ActionContext context = ActionContext.getContext();
			context.put("statementList", MemoryData.getStatementList());
		} catch (Exception e) {
			log.error("获取引导页数据", e);
		}
		return "guide";
	}

	public String footer() {
		try {
			response.getWriter().print(
					JSONArray.fromObject(MemoryData.getStatementList()));
		} catch (Exception e) {
			log.error("获取footer数据", e);
		}
		return null;
	}

	public String indexImg() {
        try {
            response.getWriter().print(
                    JSONArray.fromObject(MemoryData.getIndexImg()));
        } catch (Exception e) {
            log.error("获取indexImg数据", e);
        }
        return null;
    }
	
	public String search() {
		String type = RequestUtil.getParam(request, "type", "");
		String name = RequestUtil.getParam(request, "name", "");
		int page = RequestUtil.getParam(request, "pageNo", 1);
		int row = RequestUtil.getParam(request, "limit", 20);
		PageUtil pingouList;
		PageUtil pingiangList;
		Map<String, String> parameMap = new HashMap<String, String>();
		parameMap.put("name", name);
		parameMap.put("page", page + "");
		parameMap.put("limit", row + "");
		ActionContext.getContext().put("keywords", name);
		if ("1".equals(type)) {
			pingouList = brandService.searchByKeyWords(name, page, row);
			ActionContext.getContext().put("pingouList", pingouList);
			return "searchpingouInfo";
		} else {
			pingiangList = goodService.searchGoods(name, page, row);
			ActionContext.getContext().put("goodlist", pingiangList);
			return "searchpingInfo";
		}
	}

	public String showCategory() {
		try {
			ActionContext context = ActionContext.getContext();
			context.put("categoryList", MemoryData.getCategoryList());
			context.put("categoryId", categoryId);
			context.put("pid", pid);
			context.put("informationMap", MemoryData.getInformationMap());
			context.put("hotInfoMap", MemoryData.getHotInforMap());
			context.put("anno", annoService.findByCategory(categoryId, "0"));
			context.put("categoryInfoList", bulidCategoryList());
			context.put("categoryinforCount", categoryInforCount());
			context.put("imgInfoList", MemoryData.getImgInfoList());
			context
					.put("hotTrendCategory", MemoryData
							.getHotTrendCategoryMap());
			// 获取有关的市场信息
			List<TgMarket> marketList = marketService
					.findAllByCategoryId(categoryId);
			context.put("marketList", marketList);
			Long marketId = RequestUtil.getParam(request, "marketId", 0l);
			context.put("marketId", marketId);
			context
					.put("categoryTrend", brandService
							.findBrand(pid, -2l, 1, 3));
		} catch (Exception e) {
			log.error("显示种类页面时出错", e);
		}
		return "categoryInfo";
	}

	public String showCategoryContentList() {
		try {
			if (pageNo == 0)
				pageNo = 1;
			if (limit == 0)
				limit = 20;
			if (pageNo2 == 0)
				pageNo2 = 1;
			if (limit2 == 0)
				limit2 = 16;
			String categoryName = RequestUtil.getParam(request, "categoryName",
					"");
			ActionContext context = ActionContext.getContext();
			context.put("typeList", bulidTypeList());
			context.put("typeId", typeId);
			PageUtil pingouList;
			PageUtil pingiangList;
			Long marketId = RequestUtil.getParam(request, "marketId", 0l);
			if (0l == marketId) {
				pingouList = brandService.findBrandByCategoryIdOrTypeId(
						categoryId, typeId, pageNo, limit);
				pingiangList = goodService.findGoodsByCategoryIdORTypeId(
						categoryId, typeId, pageNo2, limit2);
			} else {
				pingouList = brandService.findBrandByCategoryIdOrTypeId(
						categoryId, typeId, pageNo, limit, marketId);
				pingiangList = goodService.findGoodsByCategoryIdORTypeId(
						categoryId, typeId, pageNo2, limit2, marketId);
			}
			context.put("marketId", marketId);
			context.put("pingouList", pingouList);
			context.put("pingiangList", pingiangList);
			context.put("categoryCount", brandService.findPingouCount(null,
					categoryId, typeId));
			context.put("categoryName", categoryName);
			context.put("anno", annoService.findByCategory(categoryId, "1"));
			context.put("goodsCount", goodService.findGoodsCount(categoryId,
					typeId));
		} catch (Exception e) {
			log.error("加载种类页面中子页面出错", e);
		}
		return "showCategoryContentList";
	}

	public String loadJoinTip() {
		PrintWriter out;
		try {
			response.reset();
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			Integer count = service.getCount();
			out.write(count + "");
		} catch (Exception e) {
			log.error("加载新增的申请", e);
		}
		return null;
	}

	public String tipOnTime() {
		PrintWriter out;
		try {
			String nowTime = RequestUtil.getParam(request, "nowTime", "");
			Integer count = annoService.findNewInfo(nowTime);
			out = response.getWriter();
			out.write(count + "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	   public String showuserip() {
	       try {
	           Vector<OnLineCustomers> list  = UserList.getInstance().getList();
	           int page = RequestUtil.getParam(request, "page", 1);
	           int row = RequestUtil.getParam(request, "list", 10);
	           int start = (page - 1) * row;
	           rows = list.subList((start > 0 ? start : 0),
                   (list.size() < ((start > 0 ? start : 0) + row) ? list
                           .size() : ((start > 0 ? start : 0) + row)));
	           total = list.size();
	        }
	        catch (Exception e) {
	            log.error("获取ip信息出错", e);
	        }
	        return Contants.JSONSTRING_RESULT_NAME;
	    }
	
	private List<TgInformation> bulidCategoryList() {
		return infoService.findByCategoryChild2(categoryId, 0, 16);
	}

	private Integer categoryInforCount() {
		return infoService.findCategoryInfoCount(categoryId);
	}

	private List<TgType> bulidTypeList() {
		return typeService.findByCategoryId(categoryId);
	}

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPageNo2() {
		return pageNo2;
	}

	public void setPageNo2(int pageNo2) {
		this.pageNo2 = pageNo2;
	}

	public int getLimit2() {
		return limit2;
	}

	public void setLimit2(int limit2) {
		this.limit2 = limit2;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

}
